package com.gientech.rocketmqproducer.Controller;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
public class ProviderController {

    @GetMapping("/syncMethod")
    public void syncMethod1() throws Exception{
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("provider");
        // 设置NameServer的地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动Producer实例
        producer.start();
        for(int i = 0; i < 10; i++){
            Message msg = new Message("TopicTest","TagA",("Hello RocketMQ [" + i + "]").getBytes(StandardCharsets.UTF_8));
            // 发送消息到一个Broker
            SendResult sentResult = producer.send(msg);
            // 打印sentResult，已确认消息是否发送成功
            System.out.printf("%s%n", sentResult);
        }
        // 如果不在发送消息，关闭Producer实例
        producer.shutdown();
    }
}
